<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 救火队队长
// +----------------------------------------------------------------------

namespace app\cardeadmin\controller;

use app\common\controller\AdminController;
use app\common\model\ManageLog;
use think\facade\Console;
use app\common\model\Message as messageModel;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;

/**
 * 平台设置
 * Class Setting
 * @package app\admin\controller
 */
class Message extends AdminController
{
    /**
     * 平台设置
     * @author 救火队队长
     */
    public function index()
    {
        if($this->request->isPost()) {
            $data = input('param.');
            $model = new messageModel();
            return $model->tableDataAdmin($data);
        }
        return View::fetch();
    }

    public function setAdd()
    {
        if($this->request->isPost()) {
            $model      = new messageModel();
            $data       = input('param.');
            $title      = input('post.title', '' , 'trim');

            $list       = Db::name('message')->where(['title'=> $title])->find();
            if($list) return $this->err('title already exist.');

            $re         = $model->setAdd($data);
            if($re){
                ManageLog::saveManageLog([
                    'manage_id' => $this->manage_id ,
                    'desc'      => 'Message Add',
                    'content'   => ['title' => $title]
                ]);
                return $this->scs('add success');
            }else{
                return $this->err('add error');
            }
        }
        return View::fetch();
    }

    public function setEdit()
    {
        $model = new messageModel();
        $id     = input('get.id');

        if($this->request->isPost()) {
            $data   = input('param.');
            $re     = $model->setEdit($data);
            if($re){
                ManageLog::saveManageLog([
                    'manage_id' => $this->manage_id ,
                    'desc'      => 'Message Edit',
                    'content'   => ['title' => $data['title']]
                ]);
                return $this->scs('edit success');
            }else{
                return $this->err('edit error');
            }
        }
        $data= $model->find($id);
        View::assign([
            'data'          => $data
        ]);
        return View::fetch();
    }

    public function delMsg()
    {
        $id = input('post.id');
        $model = new messageModel();
        $re = $model->where(['id'=>$id])->delete();
        if($re){
            ManageLog::saveManageLog([
                'manage_id' => $this->manage_id ,
                'desc'      => 'Message Del',
                'content'   => ['id' =>$id]
            ]);
            return $this->scs('Del success');
        }else{
            return $this->err('Del error');
        }
    }

    public function editStatus()
    {
        $msg_id = input('post.id');

        $model = new messageModel();
        $re = $model->changeStatus([
            'msg_id'        => $msg_id ,
            'manage_id'     => $this->manage_id
        ]);
        if($re){
            return $this->scs('Edit the successful.');
        }else{
            return $this->scs('Edit the failed.');
        }
    }

}